//
//  worksHeader.m
//  SoundPill
//
//  Created by 宋旭东 on 15/9/25.
//  Copyright © 2015年 宋旭东. All rights reserved.
//

#import "worksHeader.h"
#import "AppDelegate.h"
#define kleftGap 25 //x
#define kupGap   30 //y
#define kImageViewWidth [UIScreen mainScreen].bounds.size.width*0.28

#define kImageViewHeight [UIScreen mainScreen].bounds.size.width*0.28
#define kLabelWidth [UIScreen mainScreen].bounds.size.width*0.5
#define kLabelHeight 55

@interface worksHeader ()
{
    NSString *str;
    UIImageView *prosses;
    UIView *blackBk;
}
@end

@implementation worksHeader
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}
- (void)createSubviews
{
    UIImageView *timeLine = [[UIImageView alloc]initWithFrame:CGRectMake(16, 0, 1.5, 200)];
    timeLine.backgroundColor = [UIColor blackColor];
    [self addSubview:timeLine];
    
    //时间轴点
    UIImageView * timePoint = [[UIImageView alloc]initWithFrame:CGRectMake(9.2, 8, 15, 15)];
    timePoint.image = [UIImage imageNamed:@"gray_point"];
    [self addSubview:timePoint];
    
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    
    _imgView.image = [UIImage imageNamed:@"list_defaultImage"];
    _imgView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
    [self addSubview:_imgView];
    _imgView.frame = CGRectMake(kleftGap, kupGap, kImageViewWidth, kImageViewHeight);
    
    
    
    
    blackBk = [[UIView alloc] initWithFrame:CGRectZero];
    blackBk.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.300];
    [_imgView addSubview:blackBk];
    blackBk.frame = _imgView.bounds;
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    //_titleLabel.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.07];
    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.numberOfLines=0;
    //_titleLabel.contentMode = UIViewContentModeTop;
    if ([UIScreen mainScreen].bounds.size.width == 320) {
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    } else {
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    }
    
    [self addSubview:_titleLabel];
    _titleLabel.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width + 10, _imgView.frame.origin.y, kLabelWidth-7, 40);
    if ([UIScreen mainScreen].bounds.size.width == 320) {
        prosses = [[UIImageView alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height - 8, self.frame.size.width - (_imgView.frame.origin.x + _imgView.frame.size.width + 25), 20)];
    } else if ([UIScreen mainScreen].bounds.size.width == 375){
        prosses = [[UIImageView alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height - 3, self.frame.size.width - (_imgView.frame.origin.x + _imgView.frame.size.width + 25), 25)];
    } else {
        prosses = [[UIImageView alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, self.frame.size.width - (_imgView.frame.origin.x + _imgView.frame.size.width + 25), 25)];
    }
    [self addSubview:prosses];
    
    self.ProgressView = [[THProgressView alloc] initWithFrame:CGRectZero];
    _ProgressView.frame = prosses.frame;
    _ProgressView.progressTintColor = [UIColor blackColor];
    _ProgressView.borderTintColor = [UIColor blackColor];
    _ProgressView.progress = 0;
    [self addSubview:_ProgressView];
    
    self.classic = [[UILabel alloc] initWithFrame:CGRectZero];
    _classic.textAlignment = NSTextAlignmentRight;
    _classic.textColor = [UIColor grayColor];
    _classic.font = [UIFont boldSystemFontOfSize:13];
    _classic.text = @"草稿";
    [self addSubview:_classic];
    
    UIView *edging = [[UIView alloc] initWithFrame:CGRectZero];
    [self addSubview:edging];
    edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 94, kImageViewHeight);
    if ([UIScreen mainScreen].bounds.size.height == 667) {
        edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 115, kImageViewHeight);
    } else if ([UIScreen mainScreen].bounds.size.height == 736) {
        edging.frame = CGRectMake(_imgView.frame.origin.x + _imgView.frame.size.width, kupGap, self.frame.size.width - kleftGap - 125, kImageViewHeight);
    }
    edging.layer.borderWidth = 1;
    edging.layer.borderColor = [[UIColor colorWithWhite:0.700 alpha:1.000] CGColor];
    _classic.frame = CGRectMake(self.frame.size.width - 75, edging.frame.origin.y + edging.frame.size.height - 20, 60, 12);
    
    self.continueBtn = [[UIButton alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x, edging.frame.origin.y + edging.frame.size.height - prosses.frame.size.height - 5, 50, prosses.frame.size.height * 0.9)];
    [_continueBtn setTitle:@"继续" forState: UIControlStateNormal];
    [_continueBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _continueBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _continueBtn.layer.borderWidth = 2;
    _continueBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:_continueBtn];
    
    self.deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(_continueBtn.frame.origin.x + _continueBtn.frame.size.width + 25, edging.frame.origin.y + edging.frame.size.height - prosses.frame.size.height - 5, 50, prosses.frame.size.height * 0.9)];
    [_deleteBtn setTitle:@"删除" forState: UIControlStateNormal];
    [_deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _deleteBtn.layer.borderWidth = 2;
    _deleteBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:_deleteBtn];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    str = [self searchDraftWork];
    _titleLabel.text = @"完成度:";
        if ([[NSFileManager defaultManager] fileExistsAtPath:[str stringByAppendingPathComponent:@"full0.png"]]) {
            NSString *fullPath = [str stringByAppendingPathComponent:@"full0.png"];
            _imgView.image = [UIImage imageWithContentsOfFile:fullPath];
        } else {
            _imgView.image = [UIImage imageNamed:@"list_defaultImage"];
        }
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:[str stringByAppendingPathComponent:@"waveform.json"]] && ![[NSFileManager defaultManager] fileExistsAtPath:[str stringByAppendingPathComponent:@"imagetimeline"]]) {
//            titleLabel.text = @"完成度：30%";
//            blackBk.hidden = YES;
            prosses.image = [UIImage imageNamed:@"pro30"];
        } else {
//            titleLabel.text = @"完成度：70%";
//            blackBk.hidden = NO;
            prosses.image = [UIImage imageNamed:@"pro70"];
        }
    _continueBtn.hidden = NO;
    _deleteBtn.hidden = NO;
    _ProgressView.hidden = YES;
    prosses.hidden = NO;
    
}
- (NSString *)searchDraftWork
{
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *DouNiu = [strUrl stringByAppendingPathComponent:@"DouNiu"];
    NSArray *fileList = [[NSArray alloc] init];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    fileList = [fileManager contentsOfDirectoryAtPath:DouNiu error:&error];
    _classic.text = [NSString stringWithFormat:@"(%d)草稿", fileList.count];
    NSString *workName = [fileList lastObject];
    NSString *workPath = [DouNiu stringByAppendingPathComponent: workName];
    return workPath;
}
- (void)uploadState
{
    prosses.hidden = YES;
    _ProgressView.hidden = NO;
    self.titleLabel.text = @"正在上传";
    NSString *tagStr = [[AppDelegate shareDelegate].startTag stringByReplacingOccurrencesOfString:@"#" withString:@""];
    self.classic.text = tagStr;
    self.continueBtn.hidden = YES;
    self.deleteBtn.hidden = YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
